package com.hqd.base.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqd.base.client.model.entity.BaseGroup;
import com.hqd.base.server.mapper.BaseGroupMapper;
import com.hqd.base.server.mapper.BaseUserMapper;
import com.hqd.base.server.service.BaseGroupService;
import com.hqd.common.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author su
 * @since 2020-01-02
 */
@Service
public class GroupServiceImpl extends ServiceImpl<BaseGroupMapper, BaseGroup> implements BaseGroupService {

    @Autowired
    private BaseUserMapper baseUserMapper;
    @Autowired
    private BaseGroupMapper baseGroupMapper;


    /**
     * 根据父id查询部门
     * @param parentId
     * @return
     */
    @Override
    public List<BaseGroup> getGroupsByParentId(Integer parentId) {
        QueryWrapper<BaseGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId).eq("group_type",1);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 把查询到的父id添加到集合
     * @param groupId
     * @param list
     */
    @Override
    public void getDepartAndSubDepart(Integer groupId, List<Integer> list) {
        list.add(groupId);
        List<BaseGroup> chidlGroupList = getGroupsByParentId(groupId);
        if (CollectionUtils.isNotEmpty(chidlGroupList))
            for (BaseGroup group : chidlGroupList)
                getDepartAndSubDepart(group.getId(), list);
    }

    /**
     * 查询部门下所有员工
     * @param groupId
     * @return
     */
    @Override
    public List<Long> getAllUsersWithDepartAndSubDepart(Integer groupId) {
        List<Integer> list = new ArrayList<>();
        getDepartAndSubDepart(groupId, list);
        return this.baseUserMapper.queryAllUsersWithDepartByGroupId(list);
    }

    /**
     * tianjia
     * @param entity
     */
    @Override
    public void insertSelective(BaseGroup entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(entity.getCreateTime());
        if (-1 == entity.getParentId().intValue()) {
            entity.setPath("/" + entity.getCode());
        } else {
            QueryWrapper<BaseGroup> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parentId", entity.getParentId());
            BaseGroup baseGroup = baseGroupMapper.selectOne(queryWrapper);
            entity.setPath(baseGroup.getPath() + "/" + entity.getCode());
        }
        baseGroupMapper.insert(entity);
    }

    /**
     * xiugai
     * @param entity
     */
    @Override
    public void updateBy(BaseGroup entity) {
        if (-1 == entity.getParentId().intValue()) {
            entity.setPath("/" + entity.getCode());
        } else {
            QueryWrapper<BaseGroup> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parentId", entity.getParentId());
            BaseGroup baseGroup = baseGroupMapper.selectOne(queryWrapper);
            entity.setPath(baseGroup.getPath() + "/" + entity.getCode());
        }
        baseGroupMapper.updateById(entity);
    }

    @Override
    public BaseGroup queryGroupByUserIdAndGroupType(int paramInt1, int paramInt2) {
        return baseGroupMapper.selectGroupByUserIdAndGroupType(paramInt1,paramInt2);
    }

    /**
     * 添加领导或者是员工
     * @param groupId
     * @param members
     * @param leaders
     */
    @Override
    public void modifyGroupUsers(int groupId, String members, String leaders) {
        baseGroupMapper.deleteGroupLeadersById(groupId);
        baseGroupMapper.deleteGroupMembersById(groupId);
        if (!StringUtils.isEmpty(members)) {
            String[] mem = members.split(",");
            for (String m : mem)
                baseGroupMapper.insertGroupMembersById(groupId, Integer.parseInt(m));
        }
        if (!StringUtils.isEmpty(leaders)) {
            String[] mem = leaders.split(",");
            for (String m : mem)
                baseGroupMapper.insertGroupLeadersById(groupId, Integer.parseInt(m));
        }
    }


}
